package com.yitiao.data_algorithms.leetcode.editor.cn;

import java.util.PriorityQueue;

class KthLargestElementInAStream {
    // 2023-02-07 16:38:50
    public static void main(String[] args) {
//        KthLargest solution = new KthLargestElementInAStream().new KthLargest();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class KthLargest {

        private int limit;
        private PriorityQueue<Integer> queue;

        public KthLargest(int k, int[] nums) {
            limit = k;
            queue = new PriorityQueue<>(k);
            for (int num : nums) {
                add(num);
            }
        }

        public int add(int val) {
            if (queue.size() < limit) {
                queue.add(val);
            } else if (val > queue.peek()) {
                queue.poll();
                queue.add(val);
            }
            return queue.peek();
        }
    }

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest obj = new KthLargest(k, nums);
 * int param_1 = obj.add(val);
 */
//leetcode submit region end(Prohibit modification and deletion)

}